//
// Created by jaesonzhang on 2023/10/11.
//

#include <jni.h>
#include "CustomSkiaDraw.h"
#include "core/SkTypeface.h"
#include "core/SkFont.h"
#include "FontManager.h"
#include <android/log.h>

#define TAG "JNI_TAG"
#define LOGEE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)

CustomSkiaDraw::CustomSkiaDraw(SkBitmap bitmap) : _bitmap(bitmap) {
  background = std::make_shared<SkCanvas>(_bitmap);
}

void CustomSkiaDraw::doDraw() {
  SkPaint paint;
  paint.setColor(SK_ColorWHITE);

  SkRect rect;
  rect.set(SkIRect::MakeWH(_bitmap.width(), _bitmap.height()));
  background->drawRect(rect, paint);

  SkPaint paintTestText;
  paintTestText.setColor(SK_ColorRED);
  const char *str = "我是中文";

  SkFont skfont(SkTypeface::MakeDefault(), 90);

  SkFontMetrics *fontMetrics;
  background->drawString(SkString(str), 10, 400, skfont, paintTestText);

  // 获取系统默认字体
  std::shared_ptr<FontManager> fontManager = std::make_shared<FontManager>();
  std::string fontPath = fontManager->getDefaultFontPath();
  LOGEE("default font path:%s", fontPath.c_str());

  // 创建Skia的Typeface
  sk_sp<SkTypeface>
      skTypeface = SkTypeface::MakeFromName("sans-serif", SkFontStyle());
  // 创建Skia的Font对象
  sk_sp<SkTypeface> typeface = SkTypeface::MakeFromFile(fontPath.c_str());

  if (!typeface) {
    // 错误处理: 无法加载字体
    LOGEE("default font path:%s", "sss");
    return;
  }
  SkFont skFont(typeface, 54); // 24 is the font size

  // 创建Skia的Paint对象
  SkPaint skPaint;
  skPaint.setColor(SK_ColorBLACK);

  // 要绘制的中文文本
  const char *text = "Skia: 你好，世界！";

  // 绘制文本
  background->drawString(text, 50, 100, skFont, skPaint);

}